package zym.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Dept;
import model.Duty;
import model.Emp;
import model.EmpTransfer;
import model.LeaveOffice;
import model.LeaveType;
import util.Page;
import zym.service.TansferMag;
import dao.DeptDAO;
import dao.DutyDAO;
import dao.EmpDAO;
import dao.impl.DeptDAOImpl;
import dao.impl.DutyDAOImpl;
import dao.impl.EmpDAOImpl;
import dao.impl.LeaveTypeDAOImpl;

public class addLeave extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		TansferMag tf = new TansferMag();
		String empno=request.getParameter("empno");
		
		Date time=new Date();
		try {
			time = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("time"));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		LeaveOffice leave=new LeaveOffice();
		EmpDAO empdao=new EmpDAOImpl();
	
		int id=empdao.getIDByEmpno(empno);
		Emp emp=empdao.getEmpById(id);
		leave.setEmp(emp);
		
		LeaveType type=new LeaveType();
		int tid=Integer.parseInt(request.getParameter("ltype"));
		type=new LeaveTypeDAOImpl().getById(tid);
		leave.setLeaveType(type);
		
		String reason1=request.getParameter("reason");
		leave.setLeavereason(reason1);
		leave.setLeavetime(time);
		tf.addLeave(leave);
		request.getRequestDispatcher("/showLeave").forward(request, response);
	}

}
